﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace SimpleOrmCFPC.Core.Query.OrderBys
{
    public class OrderByComputer
    {
        public Ordering ComputeOrderBy<T, U>(Expression<Func<T, U>> orderExpr, bool asc)
        {
            if (orderExpr.NodeType == ExpressionType.Lambda)
            {
                var lambda = (LambdaExpression)orderExpr;
                var mem = lambda.Body as MemberExpression;
                if (mem != null && (mem.Expression.NodeType == ExpressionType.Parameter))
                {
                    return new Ordering
                        {
                            ColumnName = mem.Member.Name,
                            Ascending = asc
                        };
                }
                throw new NotSupportedException("Order By does not support: " + orderExpr);
            }
            throw new NotSupportedException("Must be a predicate");
        }
    }
}
